using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using PointOfSale.Global;
using System.Data.SqlClient;
using PointOfSale.POSSystem;
using PointOfSale.Class;
using PointOfSale.Languages;

namespace PointOfSale.POSSystem
{
	public class frmPOSLogin : System.Windows.Forms.Form
	{
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Button btnCancel;
		private System.Windows.Forms.Button btnLogin;

		//Cac bien dung de tao ra ket noi den co so du lieu
		private System.Windows.Forms.TextBox txtPassword;
		private System.Windows.Forms.TextBox txtUserName;
		private System.Windows.Forms.Label label2;
		private System.ComponentModel.Container components = null;

		public frmPOSLogin()
		{
			InitializeComponent();
		}
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.txtUserName = new System.Windows.Forms.TextBox();
			this.label2 = new System.Windows.Forms.Label();
			this.label1 = new System.Windows.Forms.Label();
			this.txtPassword = new System.Windows.Forms.TextBox();
			this.btnCancel = new System.Windows.Forms.Button();
			this.btnLogin = new System.Windows.Forms.Button();
			this.groupBox1.SuspendLayout();
			this.SuspendLayout();
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.txtUserName);
			this.groupBox1.Controls.Add(this.label2);
			this.groupBox1.Controls.Add(this.label1);
			this.groupBox1.Controls.Add(this.txtPassword);
			this.groupBox1.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.groupBox1.Location = new System.Drawing.Point(8, 8);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(240, 80);
			this.groupBox1.TabIndex = 0;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "LOGIN";
			// 
			// txtUserName
			// 
			this.txtUserName.Location = new System.Drawing.Point(104, 16);
			this.txtUserName.Name = "txtUserName";
			this.txtUserName.Size = new System.Drawing.Size(128, 20);
			this.txtUserName.TabIndex = 0;
			this.txtUserName.Text = "";
			this.txtUserName.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtUserName_KeyPress);
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(8, 24);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(96, 16);
			this.label2.TabIndex = 6;
			this.label2.Text = "User Name";
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(8, 56);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(72, 16);
			this.label1.TabIndex = 4;
			this.label1.Text = "Password";
			// 
			// txtPassword
			// 
			this.txtPassword.Location = new System.Drawing.Point(104, 48);
			this.txtPassword.Name = "txtPassword";
			this.txtPassword.PasswordChar = '*';
			this.txtPassword.Size = new System.Drawing.Size(104, 20);
			this.txtPassword.TabIndex = 2;
			this.txtPassword.Text = "";
			this.txtPassword.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtPassword_KeyPress);
			// 
			// btnCancel
			// 
			this.btnCancel.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnCancel.Location = new System.Drawing.Point(168, 96);
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.TabIndex = 3;
			this.btnCancel.Text = "Cancel";
			this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
			// 
			// btnLogin
			// 
			this.btnLogin.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnLogin.Location = new System.Drawing.Point(96, 96);
			this.btnLogin.Name = "btnLogin";
			this.btnLogin.Size = new System.Drawing.Size(64, 24);
			this.btnLogin.TabIndex = 2;
			this.btnLogin.Text = "Login";
			this.btnLogin.Click += new System.EventHandler(this.btnLogin_Click);
			// 
			// frmPOSLogin
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(256, 134);
			this.Controls.Add(this.btnLogin);
			this.Controls.Add(this.btnCancel);
			this.Controls.Add(this.groupBox1);
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "frmPOSLogin";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "POS Login";
			this.Load += new System.EventHandler(this.frmPOSLogin_Load);
			this.groupBox1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion


		private string encryptString(string strToEncrypt)
		{
			System.Text.UTF8Encoding ue = new System.Text.UTF8Encoding();
			byte[] bytes = ue.GetBytes(strToEncrypt);
  
			// encrypt bytes
			System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
			byte[] hashBytes = md5.ComputeHash(bytes);
  
			// Convert the encrypted bytes back to a string (base 16)
			string hashString = "";
  
			for(int i=0;i<hashBytes.Length;i++)
			{
				hashString += Convert.ToString(hashBytes[i],16).PadLeft(2,'0');
			}
  
			return hashString.PadLeft(32,'0');
		}
	
		private void btnLogin_Click(object sender, System.EventArgs e)
		{
			string strUserName = "";
			string strPassword = "";
			strUserName = this.txtUserName.Text.Trim();
			strPassword = this.encryptString(this.txtPassword.Text.Trim());
			if(strUserName.Equals(""))
			{
				MessageBox.Show(clsGlobal.g_Hashtable["msgEmptyUserName"].ToString());
				this.txtUserName.Clear();
				this.txtPassword.Clear();
			}
			else
				try
				{
					string SelectCommand  = "SELECT * FROM tblUser WHERE UserName = '" + strUserName.Trim() + "'";
					SqlCommand _sqlCommand = new SqlCommand(SelectCommand, clsGlobal.g_DataConn);
					SqlDataReader dr = _sqlCommand.ExecuteReader();
					int i = 0;

					while(dr.Read())
					{
						i++;
						if(!strPassword.Equals(dr["Password"].ToString()))
						{
							MessageBox.Show(clsGlobal.g_Hashtable["msgWrongPassword"].ToString());
							this.txtPassword.Clear();
							this.txtPassword.Focus();
							
						}			
						else
						{
							clsGlobal.g_UserName = strUserName;
							clsGlobal.g_UserId = System.Convert.ToInt32(dr["UserId"].ToString());
							clsGlobal.g_DepartmentId = System.Convert.ToInt32(dr["DepartmentId"].ToString());
							clsGlobal.g_UserFullName = dr["UserFullName"].ToString();

							if(clsGlobal.g_isChangeUser)
							{
								clsGlobal.g_UserPrivilige = System.Convert.ToBoolean(dr["Privilige"].ToString());
								mainForm.Call delInstance = new mainForm.Call(mainForm.CallMethod);
								delInstance(clsGlobal.g_UserName);
							}
							else
							{
								clsGlobal.g_UserPrivilige = System.Convert.ToBoolean(dr["Privilige"].ToString());	
							}
							this.Visible = false;
							clsGlobal.g_TimeIn = System.DateTime.Now.ToString().Split(' ')[0] + " " + System.DateTime.Now.ToString().Split(' ')[1];
						}
					}
					dr.Close();
					if(clsGlobal.g_DepartmentId != 0)
					{
						clsGlobal.g_DepartmentName = clsDepartment.GetDepartmentName(clsGlobal.g_DepartmentId);
						this.Close();
					}
					if(i == 0)
					{
						MessageBox.Show(clsGlobal.g_Hashtable["msgNotExistedUserName"].ToString());
						this.txtUserName.Clear();
						this.txtPassword.Clear();
						this.txtUserName.Focus();
					}
				}
				catch(Exception ex)
				{
					MessageBox.Show(ex.ToString());
				}
		}

		private void btnCancel_Click(object sender, System.EventArgs e)
		{
			Environment.Exit(1);
		}

		private void frmPOSLogin_Load(object sender, System.EventArgs e)
		{
			//Read value from INI
			if(!clsGlobal.g_isChangeUser)
			{
				cIni objIni = new cIni(clsGlobal.g_AppPath + clsGlobal.g_INI_FILE_NAME);
				clsGlobal.g_ServerIPAddress = objIni.ReadValue("ServerConfig", "ServerIPAddress", "");
				clsGlobal.g_SQLUserName = objIni.ReadValue("ServerConfig", "SQLUserName", "");
				clsGlobal.g_SQLPassword = objIni.ReadValue("ServerConfig", "SQLPassword", "");

				clsGlobal.g_DatabaseName = "POS";
				clsGlobal.g_DataConn = clsGlobal.Db.ConnectToDatabase (clsGlobal.g_DatabaseName,clsGlobal.g_ServerIPAddress,clsGlobal.g_SQLUserName,clsGlobal.g_SQLPassword);
			}

			this.groupBox1.Text = clsGlobal.g_Hashtable["LoginGroup"].ToString();
			this.label2.Text = clsGlobal.g_Hashtable["LoginUserName"].ToString();
			this.label1.Text = clsGlobal.g_Hashtable["LoginPassword"].ToString();
			this.btnCancel.Text = clsGlobal.g_Hashtable["LoginbtnCancel"].ToString();
			this.btnLogin.Text = clsGlobal.g_Hashtable["LoginbtnLogin"].ToString();
			this.Text = clsGlobal.g_Hashtable["LoginFormName"].ToString();
			
		}

		private void txtUserName_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
		{
			if(e.KeyChar == 13)
			{
				this.txtPassword.Focus();
			}
		}

		private void txtPassword_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
		{
			if(e.KeyChar == 13)
			{
				btnLogin_Click(sender, e);
			}
		}
	}
}